define(function (require,exports,module) {
    var angular = require('angular');

    var material = angular.module('material.videoController',[]);

    require('business_modules/material/modules/video/videoService');
    require('business_modules/material/modules/uploaderService');

    material.controller('video',[
        '$scope',
        '$rootScope',
        'uploaderService',
        'videoService',
        'limit',
        function ($scope,$rootScope,uploaderService,videoService,limit) {

            $scope.videoDelete = {
                title: "确定删除此素材？",
                followType: "text",
                ok: function (index) {
                    $scope.deleteVideo($scope.videoList[index].id,index);
                }
            };

            $scope.pagination = {
                pages : 0,
                current : 1,
                change: function (page) {
                    $scope.searchVideoList(page);
                }
            };

            $scope.searchVideoList = function (page) {
                videoService.getVideoList({
                    search:{
                        limit: limit,
                        offset: limit * (page - 1)
                    },
                    success: function (res) {
                        $scope.videoList = res.data.data;
                        $scope.videoCount = res.data.total;
                        $scope.pagination.pages = Math.ceil(res.data.total / limit);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','获取视频列表失败');
                    }
                });
            };

            $scope.deleteVideo = function (videoId) {
                videoService.deleteVideo({
                    search:{
                        id: videoId
                    },
                    success: function (res) {
                        $scope.searchVideoList($scope.pagination.current);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','删除视频列败');
                    }
                });
            };

            $scope.searchVideoList(1);
        }
    ]);
});
